package com.eqdd.yiqidian.ui.discover.makerspace

import android.support.v4.content.ContextCompat
import android.support.v7.widget.LinearLayoutManager
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import android.text.style.RelativeSizeSpan
import android.view.LayoutInflater
import android.view.View
import com.eqdd.yiqidian.R
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector
import com.eqdd.yiqidian.base.BaseActivity
import com.eqdd.yiqidian.base.Config
import com.eqdd.yiqidian.box.ItemDecoration.DividerLineItemDecoration
import com.eqdd.yiqidian.helper.mvchelper.ModelRx2DataSource
import com.eqdd.yiqidian.http.HttpConfig
import com.eqdd.yiqidian.http.HttpResult
import com.eqdd.yiqidian.http.JsonConverter
import com.eqdd.yiqidian.kotlin.deleteDialog
import com.eqdd.yiqidian.ui.yiqixue.info.LectureInfoActivity
import com.example.library.AutoFlowLayout
import com.example.library.FlowAdapter
import com.lzy.okgo.OkGo
import com.lzy.okrx2.adapter.FlowableBody
import com.shizhefei.mvc.MVCCoolHelper
import io.reactivex.Flowable
import kotlinx.android.synthetic.main.activity_recyclerview_refresh.*
import kotlinx.android.synthetic.main.layout_flow_tag.view.*
import org.jetbrains.anko.startActivity
import java.util.*

/**
 * Created by jhj on 18-8-13.
 */
class CollectCourseActivity : BaseActivity() {
    override fun initBinding() {
        setContentView(R.layout.activity_recyclerview_refresh)
        initTopTitleBar(View.VISIBLE, "收藏的课程")
    }

    override fun initData() {
        var pageNum = 0
        val list = arrayListOf<MakerBean>()
        val mvcHelper = MVCCoolHelper<List<MakerBean>>(coolRefreshView)
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.addItemDecoration(DividerLineItemDecoration(this))
        val slimAdapterEx = SlimAdapterEx.create()
                .register<MakerBean>(R.layout.list_item_183, SlimInjector<MakerBean> { data, injector ->
                    val str = String.format(Locale.CHINA, "%s(%s)", data?.courseName, data?.lectureName)
                    val span = SpannableString(str)
                    span.setSpan(ForegroundColorSpan(ContextCompat.getColor(this, R.color.hint_color)), data?.courseName?.length
                            ?: 0, str.length, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE)
                    span.setSpan(RelativeSizeSpan(0.8f), data?.courseName?.length
                            ?: 0, str.length, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE)
                    injector?.image(R.id.imageView, data?.courseImg)
                            ?.text(R.id.tv_name, span)
                            ?.with<AutoFlowLayout<String>>(R.id.autoFlowLayout) {
                                it.removeAllViews()
                                val type = data?.courseType?.split(",")
                                if (type?.size ?: 0 > 0) {
                                    it.visibility = View.VISIBLE
                                } else {
                                    it.visibility = View.GONE
                                }
                                it.setAdapter(object : FlowAdapter<String>(type) {
                                    override fun getView(p0: Int): View {
                                        val view = LayoutInflater.from(this@CollectCourseActivity).inflate(R.layout.layout_flow_tag, null)
                                        view.tv_tag.text = type?.get(p0)
                                        return view
                                    }
                                })
                            }
                })
                .attachTo(recyclerView)
                .updateData(list as List<MakerBean>?) as SlimAdapterEx<MakerBean>?

        ItemClickSupport.addTo(recyclerView)
                .setOnItemClickListener { recyclerView, position, v ->
                    val data = slimAdapterEx?.getDataItem(position)
                    startActivity<LectureInfoActivity>(Config.ID to data?.courseId)
                }
                .setOnItemLongClickListener { recyclerView, position, v ->
                    val data = slimAdapterEx?.getDataItem(position)
                    deleteDialog(HttpConfig.BASE_URL + HttpConfig.MAKER_COLLECT_CANCEL, "是否取消收藏“${data?.courseName}”？", "collectId" to data?.Id, "type" to 2, isFinish = false) {
                        mvcHelper.refresh()
                    }
                    true
                }

        val dataSource = ModelRx2DataSource<MakerBean>(ModelRx2DataSource.OnLoadSource<MakerBean> { page, register ->
            if (page == 1) {
                pageNum = 0
            }
            OkGo.post<HttpResult<List<MakerBean>>>(HttpConfig.BASE_URL + HttpConfig.MAKER_COLLECT_COURSE)
                    .params("userGuid", user.guid)
                    .params("page", pageNum)
                    .converter(object : JsonConverter<HttpResult<List<MakerBean>>>() {
                        override fun test() {
                            super.test()
                        }
                    })
                    .adapt(FlowableBody<HttpResult<List<MakerBean>>>())
                    .flatMap({ result ->
                        if (pageNum == 0) {
                            mvcHelper.isAutoLoadMore = true
                        }
                        pageNum = result.page
                        Flowable.just(if (result.items == null) arrayListOf<MakerBean>() else result.items)
                    })
        })

        mvcHelper.setDataSource(dataSource)
        mvcHelper.adapter = slimAdapterEx
        mvcHelper.refresh()

    }

    override fun setView() {

    }


    class MakerBean {
        val Id: Int = 0
        val courseId: Int = 0
        val courseImg: String = ""
        val courseName: String = ""
        val courseType: String = ""
        val lectureName: String = ""
    }
}